var EXPORTED_SYMBOLS = ["DMS_GetAll"];

DMS_GetAll = function(){

};

Components.utils.import("resource://SynoLoader/Command.js");  
Components.utils.import("resource://SynoLoader/Util.js");  
Components.utils.import("resource://SynoLoader/DMS_Commando/DMS_Connect.js"); 
Components.utils.import("resource://SynoLoader/DMS_Commando/DMS_TaskAktion.js");
Components.utils.import("resource://gre/modules/Dict.jsm");

DMS_GetAll = new Command();
DMS_GetAll.constructor = DMS_GetAll;
DMS_GetAll.items = new Dict();

DMS_GetAll.mediator = Components.classes['@mozilla.org/appshell/window-mediator;1']
                  .getService(Components.interfaces.nsIWindowMediator);
DMS_GetAll.document = DMS_GetAll.mediator.getMostRecentWindow("navigator:browser").document;


DMS_GetAll.getlist = function()
{
	return DMS_GetAll.items.listvalues();
};

DMS_GetAll.onsuccess	= function(http_request)
{
  	var mpInfo = JSON.parse(http_request.responseText); 
	if (mpInfo.success == false)
	{
		Util.log("GetAll Faild!\n" + mpInfo.errno.section +" : " +mpInfo.errno.key);  
		DMS_Connect.send();
	}
	else    
	{
		Util.log("GetAll Succeeded !\n");
		DMS_TaskAktion.url=this.url;
		this.DownloadInfo=mpInfo;
		this.calcItems(mpInfo);
	}	
} ; 


DMS_GetAll.timeoutfunction= function()
{
	Util.log("GetAll timeOut");
};

DMS_GetAll.calcItems=function(data)
{
	var listkeys = DMS_GetAll.items.listkeys();
	for (i in listkeys)
	{
		var findId=false;
		for(u in data.items)
		{
		 	if(listkeys[i]=="DMS" + data.items[u].task_id)
				findId=true;
		}
		if(!findId)
			this.items.del(listkeys[i]);
		
		
	}
	for(i in data.items)
	{	
		 var richlistitem = DMS_GetAll.document.createElement('richlistitem');
		 var vbox = DMS_GetAll.document.createElement('vbox');
		 var hbox = DMS_GetAll.document.createElement('hbox');
		 var title = DMS_GetAll.document.createElement('label');
		 var progressmeter = DMS_GetAll.document.createElement('progressmeter');
		 var label = DMS_GetAll.document.createElement('label');
		 var start = DMS_GetAll.document.createElement('toolbarbutton');
		 var del = DMS_GetAll.document.createElement('toolbarbutton');
		 
		 start.setAttribute('id', "syno-start" + data.items[i].task_id);
		 start.setAttribute('class',"SynoLoader_Item_start");
		 start.setAttribute('autocheck',"false");
		 
		 start.status=data.items[i].status;
		 start.addEventListener('command', DMS_GetAll.OnStart, true);	
		 if (data.items[i].status=="TASK_PAUSED")
		 {
		       start.setAttribute('style', "list-style-image: url('chrome://SynoLoader/skin/Play.png')");       
		 }
		 else
		 {
		       start.setAttribute('style', "list-style-image: url('chrome://SynoLoader/skin/Pause.png')");
		 }
		 del.setAttribute('autocheck',"false");
		 del.setAttribute('id', "syno-del" + data.items[i].task_id);
		 del.setAttribute('class',"SynoLoader_Item_del");
		 del.setAttribute("style", "list-style-image: url('chrome://SynoLoader/skin/Stop.png')");
		 del.addEventListener('command', DMS_GetAll.OnDel, true);		     
		 
		 progressmeter.setAttribute('value', data.items[i].progress.replace("%",""));
		 progressmeter.setAttribute('class',"SynoLoader_Item_progress");
		 progressmeter.setAttribute('id', "syno-progress" + data.items[i].task_id);
		 progressmeter.setAttribute('flex','1');
		 title.setAttribute('id', "syno-title" + data.items[i].task_id);
		 title.setAttribute('value', data.items[i].filename);
		 title.setAttribute('crop', "center"); 
		 title.setAttribute('class',"SynoLoader_Item_title");
		 
		 label.setAttribute('value', data.items[i].status + " - " + data.items[i].current_size +" of "+ data.items[i].total_size + " - " + data.items[i].progress );
		 label.setAttribute('id', "syno-label" + data.items[i].task_id);
		 label.setAttribute('crop', "center");


		 hbox.setAttribute('class',"SynoLoader_Item_hbox");
		 hbox.setAttribute('flex','1');
		 vbox.setAttribute('flex','1');
		 vbox.appendChild(title);
		
		 hbox.appendChild(progressmeter);
		 hbox.appendChild(start);
		 hbox.appendChild(del);
		 vbox.appendChild(hbox);
		 vbox.appendChild(label);
		 
		 richlistitem.setAttribute('id',"syno-" + data.items[i].task_id);
		 richlistitem.setAttribute('syono-id',data.items[i].task_id);
		 richlistitem.setAttribute('class',"SynoLoader_Item");
		 richlistitem.appendChild(vbox);
		 
		 this.items.set("DMS" + data.items[i].task_id,richlistitem);
		
	}	
	
};

DMS_GetAll.OnStart = function(event)
{            

     if (event.target.status=="TASK_PAUSED")
	     {
		        DMS_TaskAktion.setParameter("resume",event.target.id.replace("syno-start",""));
				DMS_TaskAktion.send();
	     }
	     else
	     {
		        DMS_TaskAktion.setParameter("stop",event.target.id.replace("syno-start",""));
				DMS_TaskAktion.send();
	     }
};

DMS_GetAll.OnDel = function(event)
{

	 DMS_TaskAktion.setParameter("delete",event.target.id.replace("syno-del",""));
	 DMS_TaskAktion.send();
};
 

DMS_GetAll.parameter='action=getall&start=0&limit=25&sort=task_id&dir=ASC&filterid=all';


